//
//  ItemLargerViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/11/21.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "ItemLargerViewController.h"
#import "LargerReportModel.h"
#import "ReportCustomHeadView.h"
#import "LargerReportTableViewCell.h"
#import "ReportLargerHeadView.h"
#import "BaseSearchChooseViewController.h"
@interface ItemLargerViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)LargerReportModel *model;

@property(nonatomic,strong)TB_TableView *homeTableView;

@property(nonatomic,strong)ReportCustomHeadView *headView;


@end

@implementation ItemLargerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"检查大项报告";
    
    [USER_DEFAULT setObject:@[] forKey:@"TBChoose"];//默认滞空
    
    [USER_DEFAULT setObject:@[] forKey:@"TBReqChoose"];//默认滞空
    
    [self.view addSubview:self.headView];
    
    [self.view addSubview:self.homeTableView];
    
    
      [self addNavigationItemWithTitles:@[@"筛选"] isLeft:NO target:self action:@selector(PushChangeStatueVC) tags:nil];
    
    [self loadDatas];
    // Do any additional setup after loading the view.
}

-(ReportCustomHeadView*)headView
{
    if (!_headView) {
        
        _headView=[[ReportCustomHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50) andCount:4];
        
        
        
    }
    
    return _headView;
}


-(void)loadDatas
{
    
    [self showLoadingAnimation];
    
    BaseReqApi *Api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/bigItemReprot.json" andrequestTime:5 andParams:self.ReqModel.mj_keyValues andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [Api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        
        [self stopLoadingAnimation];
        
        self.model=[LargerReportModel mj_objectWithKeyValues:responseObject];
        
        if (responseStatus==1) {
            //请求成功
            if (self.model.data.count==0) {
                
                self.Type=TBNODateType;
                
            }
            
            [self.homeTableView reloadData];
            
        }else if (responseStatus==0||responseStatus==2)
        {
            [MBProgressHUD showErrorMessage:message];
            
            self.Type=TBReqFalie;
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
            
        }
        
    }];
    
    
    
}




-(TB_TableView*)homeTableView
{
    if (!_homeTableView) {
        
        _homeTableView=[[TB_TableView alloc]initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, SCREEN_HEIGHT-64-50) style:UITableViewStylePlain];
        
        _homeTableView.delegate=self;
        
        _homeTableView.dataSource=self;
        
        _homeTableView.estimatedRowHeight=40;
        
        [_homeTableView registerNib:[UINib nibWithNibName:@"LargerReportTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
        
        [_homeTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell1"];
        
        _homeTableView.tableFooterView=[[UIView alloc]init];

        
    }
    
    return _homeTableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.model.data.count;
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 2;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    LargerReportData *data=self.model.data[indexPath.section];
    
    if (indexPath.row==0) {
        
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        
         cell.textLabel.numberOfLines=0;
        
        cell.textLabel.text=data.typeName;
 
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        return cell;
        
        
    }else{
        
        LargerReportTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        
        cell.data=data;
        
        return cell;
        
    }
    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row==0) {
        
        return  UITableViewAutomaticDimension;
        
        
    }else{
        
        
        return 50;
    }
    
}



#pragma mark- 搜索跳转事件
-(void)PushChangeStatueVC
{
    
    BOOL isSelf=self.ReqModel.shopId==0?YES:NO;
    
    BaseSearchChooseViewController *Choose=[[BaseSearchChooseViewController alloc]init];
    
    Choose.ReqStr=[SearchAndChooseManager getChooseUrlWithType:bigItemReprotType];
    
    Choose.CustomArr=[SearchAndChooseManager GetChooseDateWithType:bigItemReprotType andIsLookSelf:isSelf];
    
    Choose.SearchPlace=@"搜索关键字";
    
 
     Choose.Lshopid=[self.ReqModel.shopId integerValue];
    
    Choose.isHiddenSearch=YES;
    
    Choose.SureBlock=^(NSString *paras,NSString*keyword)
    {
     
        
        self.ReqModel.params=paras;

           [self loadDatas];
        
    };
    
    [self.navigationController pushViewController:Choose animated:YES];
    
}

- (void)actionTapEmptyDataSetNodataView
{
    
    [self loadDatas];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
